//
//  PinJiaCell.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/19.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "PinJiaCell.h"

@implementation PinJiaCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[_dataDic objectForKey:@"avatar"]];
    [self.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
    self.images.layer.masksToBounds = YES;
    [self.images.layer setCornerRadius:23/2.0];
    
    _labName.text = _dataDic[@"nickname"];
    _labName.numberOfLines = 0;
    [_labName mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(ScreenWidth - 120);
        make.height.mas_equalTo([self heightWithTextFont:13 andWidth:ScreenWidth - 120 andContent:_dataDic[@"nickname"]].height + 5);
    }];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy.MM.dd"];
    
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:[_dataDic[@"commenttime"] doubleValue]];
    NSString *dateString2 = [formatter stringFromDate:date2];
    NSLog(@"结束时间: %@", dateString2);
    
    
    StarView *starView = [[StarView alloc] init];
    starView.showStar = [_dataDic[@"star"] integerValue] * 20;
    [_starView addSubview:starView];
    
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    _labDate.text = dateString2;
    
    _labText.text = _dataDic[@"comment"];
    [_labText mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(ScreenWidth - 70);
        make.height.mas_equalTo([self heightWithTextFont:13 andWidth:ScreenWidth - 70 andContent:_dataDic[@"comment"]].height + 5);
    }];
    
}

//根据内容字体的大小和行宽计算文本的高度
-(CGSize )heightWithTextFont:(CGFloat)font andWidth:(CGFloat )textWidth andContent:(NSString *)text
{
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
    
    CGSize rectSize=[text boundingRectWithSize:CGSizeMake(textWidth, 0) options:\
                     NSStringDrawingTruncatesLastVisibleLine |
                     NSStringDrawingUsesLineFragmentOrigin |
                     NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return rectSize;
}

@end
